package com.hhit.bigscreenbackmanage.service.impl.jjgl;

import com.alibaba.fastjson.JSONObject;
import com.hhit.bigscreenbackmanage.dao.jjgl.TJjglWgglMxMapper;
import com.hhit.bigscreenbackmanage.dao.jjgl.TJjglWgglTjMapper;
import com.hhit.bigscreenbackmanage.entity.jjgl.TJjglWgglMxEntity;
import com.hhit.bigscreenbackmanage.entity.jjgl.TJjglWgglTjEntity;
import com.hhit.bigscreenbackmanage.service.jjgl.TJjglWgglMxService;
import com.hhit.bigscreenbackmanage.util.PageUtil;
import com.hhit.bigscreenbackmanage.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TJjglWgglMxServiceImpl implements TJjglWgglMxService {

    private Logger log = LoggerFactory.getLogger(TJjglWgglMxServiceImpl.class);

    private static String[] cityArr = {"建设分公司", "南京", "苏州", "无锡", "徐州", "常州", "镇江", "扬州", "泰州", "南通", "盐城", "淮安", "宿迁", "连云港"};

    @Resource
    TJjglWgglMxMapper tJjglWgglMxMapper;

    @Resource
    TJjglWgglTjMapper tJjglWgglTjMapper;

    @Override
    public Map<String, Object> importWgglMx(String propName, MultipartFile[] files, String dataTime) {
        Map<String,Object> resultMap = new HashMap<>();
        boolean flag = false;
        try {
            List<Map<String,Object>> result = Utils.getExcelData(propName,files);
            if(result.size() > 0 ){
                //将Map转为Bean
                List<TJjglWgglMxEntity> list = (List<TJjglWgglMxEntity>)Utils.mapToEntiry(result, TJjglWgglMxEntity.class);
                if(list.size() > 0 ){
                    //处理实际投运时间
                    for (int i = 0; i < list.size(); i++) {
                        TJjglWgglMxEntity TJjglWgglMxEntity = list.get(i);
                        if(!TJjglWgglMxEntity.getSjtysj().contains("-")&& TJjglWgglMxEntity.getSjtysj()!=null&&!"".equals(TJjglWgglMxEntity.getSjtysj())){
                            TJjglWgglMxEntity.setSjtysj("");
                        }
                        if(!TJjglWgglMxEntity.getGktysj().contains("-")&& TJjglWgglMxEntity.getGktysj()!=null&&!"".equals(TJjglWgglMxEntity.getGktysj())){
                            TJjglWgglMxEntity.setGktysj("");
                        }
                        TJjglWgglMxEntity.setDrsj(dataTime);
                    }
                    tJjglWgglMxMapper.deleteWgglMx(dataTime);
                    int count =  tJjglWgglMxMapper.importWgglMx(list);
                    if(count > 0){
                        flag = true;
                    }
                }
            }
            resultMap.put("flag",flag);
        }catch (Exception e) {
            resultMap.put("flag",false);
            e.printStackTrace();
        }
        return resultMap;
    }

    @Override
    public Map<String,Object> getWgglmx(JSONObject jsonObject) {
        Map<String,Object> out = new HashMap<>();
        PageUtil page = new PageUtil();
        String pageSize = jsonObject.get("pageSize").toString();
        String pageNo = jsonObject.get("pageNo").toString();
        int count = tJjglWgglMxMapper.queryAllCount(jsonObject);
        page.pageCount=Integer.parseInt(pageSize);// 每页数
        page.init(count,Integer.parseInt(pageNo));//参数1：总行数，参数2：当前页
        jsonObject.put("rowNum",page.getRowNum());
        jsonObject.put("pageCount",page.getPageCount());
        out.put("page",page);
        out.put("list", tJjglWgglMxMapper.queryAll(jsonObject));
        return out;
    }

    @Override
    public Map<String, Object> getWggltj(JSONObject jsonObject) {
        Map result = new HashMap();
        List<String> wgxm = new ArrayList<>();
        List<Map<String,Object>> wgxmList = tJjglWgglMxMapper.getWgxmBarList();
        if (wgxmList.size()>0){
            addNum(wgxmList,wgxm);
        }else{
            addZero(wgxm);
        }
        List<String> bqwcwgxm = new ArrayList<>();
        List<Map<String,Object>> bqwcwgxmList = tJjglWgglMxMapper.getBqwcwgxmBarList();
        if (bqwcwgxmList.size()>0){
            addNum(bqwcwgxmList,bqwcwgxm);
        }else{
            addZero(bqwcwgxm);
        }
        List<String> cqwgxm = new ArrayList<>();
        List<Map<String,Object>> cqwgxmList = tJjglWgglMxMapper.getCqwgxmBarList();
        if (cqwgxmList.size()>0){
            addNum(cqwgxmList,cqwgxm);
        }else{
            addZero(cqwgxm);
        }
        result.put("wgxm",wgxm);
        result.put("bqwcwgxm",bqwcwgxm);
        result.put("cqwgxm",cqwgxm);
        return result;
    }

    /**
     * 数组补0
     * @param arr
     */
    private void addZero(List<String> arr){
        for (int i = 0; i < cityArr.length; i++) {
            arr.add("0");
        }
    }

    /**
     * 添加数据
     * @param mapList
     * @param arr
     */
    private void addNum(List<Map<String,Object>> mapList,List<String> arr){
        for (int i = 0; i < cityArr.length; i++) {
            String city = cityArr[i];
            int x = 0;
            for (Map map:mapList) {
                Object dsgs = map.get("dsgs");
                if("建设分".equals(dsgs)){
                    dsgs = "建设分公司";
                }
                if(city.equals(dsgs)){
                    arr.add(map.get("num").toString());
                    x = 1;
                    break;
                }else {
                    continue;
                }
            }
            if (x==0){
                arr.add("0");
            }
        }
    }
}
